package com.cykj.web.controller.posbiz;

import com.alibaba.fastjson.JSONObject;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysDept;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.core.page.AppTableData;
import com.cykj.common.core.page.TableData;
import com.cykj.common.core.page.TableDataInfo;
import com.cykj.common.enums.BusinessType;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.*;
import com.cykj.pos.mapper.BizMerchantInfoMapper;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.service.IBizMerchTransactionsService;
import com.cykj.pos.util.HttpUtil;
import com.cykj.system.mapper.SysDeptMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 商户交易记录Controller
 *
 * @author ningbingwu
 * @date 2021-01-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Mapper
@RequestMapping("/merchant/transactions")
@Api(tags = "商户交易记录")
public class BizMerchTransactionsController extends BaseController {

    private final IBizMerchTransactionsService iBizMerchTransactionsService;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Autowired
    BizPosMachineMapper posMachineMapper;
    @Resource
    private PosPCommonParamConfig PosPCommonParamConfig;
    @Resource
    private BizMerchantInfoMapper bizMerchantInfoMapper;

    /**
     * 查询商户交易记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizMerchTransactions bizMerchTransactions) {
        startPage();
        List<BizMerchTransactions> list = iBizMerchTransactionsService.queryList(bizMerchTransactions);
        return getDataTable(list);
    }

    /**
     * @author: haoran
     * @date: 2022/7/28
     * 方法名称： 所有交易记录列表
     */
    @GetMapping("/lists")
    public TableDataInfo lists(BizMerchTransactions bizMerchTransactions) {
//        startPage();
        List<BizMerchTransactions> list = iBizMerchTransactionsService.queryLists(bizMerchTransactions);
        return getDataTable(list);
    }

    /**
     * @author: haoran
     * @date: 2022/7/29
     * 方法名称： 交易记录
     */
    @GetMapping("/lists/partner1")
    public TableDataInfo listsPartner1(BizMerchTransactions bizMerchTransactions) {
//        startPage();
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listsPartner1(bizMerchTransactions);
        return getDataTable(list);
    }

    /*所有交易记录*/
    @GetMapping("/turnover")
    public BizMerchBillTurnoverDTO turnoverlist(BizMerchTransactions bizMerchTransactions) {
        BizMerchBillTurnoverDTO bizMerchBillTurnoverDTO = iBizMerchTransactionsService.turnoverlist(bizMerchTransactions);
        return bizMerchBillTurnoverDTO;
    }
    /*不要了，，废弃*/
    @GetMapping("/lists/partner")
    public TableDataInfo listsPartner(BizMerchTransactions bizMerchTransactions) {
        startPage();
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listsPartner(bizMerchTransactions);
        return getDataTable(list);
    }
    /*追溯记录*/
    @GetMapping("/lists/asscend")
    public TableDataInfo listsAsscend(BizMerchTransactions bizMerchTransactions) {
//        startPage();
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listsAsscend(bizMerchTransactions);
        return getDataTable(list);
    }
    @Log(title = "追溯记录导出", businessType = BusinessType.EXPORT)
    @GetMapping("/asscendExport")
    public AjaxResult asscendExport(BizMerchTransactions bizMerchTransactions) {
        /*这个导出有问题暂时改成*/
        bizMerchTransactions.setPageSize(10000000);
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listsAsscend(bizMerchTransactions);
        ExcelUtil<BizMerchTransactions> util = new ExcelUtil<BizMerchTransactions>(BizMerchTransactions.class);
        //调用数据安全接口
        JSONObject legalPerson = new JSONObject();
        legalPerson.put("agentid", this.agentid());
        HttpUtil.doPostxxl(PosPCommonParamConfig.getClosurePath(), JSONObject.toJSONString(legalPerson));
        return util.exportExcel(list, "transactions");
    }
    /*返现列表*/
    @GetMapping("/lists/return")
    public TableDataInfo listsReturn(BizMerchTransactions bizMerchTransactions) {
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listsReturn(bizMerchTransactions);
        return getDataTable(list);
    }

    /*流量费费列表*/
    @GetMapping("/lists/tramcBilling")
    public TableDataInfo tramcBilling(BizFeesChargedDTO bizFeesChargedDTO) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysDept dept=sysDeptMapper.selectDeptById(sysUser.getDeptId());
        if(!dept.getDeptName().equals("支付助手总部")){
            bizFeesChargedDTO.setOrgnname(dept.getDeptName());
            Long merchId = posMachineMapper.selectMerchId(sysUser.getUserId());
            bizFeesChargedDTO.setMerchId(merchId);
        }

        startPage();
        List<BizFeesChargedDTO> list = iBizMerchTransactionsService.tramcBilling(bizFeesChargedDTO);
        return getDataTable(list);
    }
    /**
     * 导出商户交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:transactions:export')")
    @Log(title = "商户交易记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizMerchTransactions bizMerchTransactions) {
        /*这个导出有问题暂时改成*/
        bizMerchTransactions.setPageSize(10000000);
        List<BizMerchTransactions> list = iBizMerchTransactionsService.queryLists(bizMerchTransactions);
        ExcelUtil<BizMerchTransactions> util = new ExcelUtil<BizMerchTransactions>(BizMerchTransactions.class);
        //调用数据安全接口
        JSONObject legalPerson = new JSONObject();
        legalPerson.put("agentid", this.agentid());
        HttpUtil.doPostxxl(PosPCommonParamConfig.getClosurePath(), JSONObject.toJSONString(legalPerson));
        return util.exportExcel(list, "transactions");
    }
    private String agentid(){
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        String suffix=bizMerchantInfoMapper.getSuffix(sysUserman.getUserId());
        return bizMerchantInfoMapper.getAgentId(suffix);
    }
    @Log(title = "流量费列表", businessType = BusinessType.EXPORT)
    @GetMapping("/lists/tramcBilling/export")
    public AjaxResult exportBilling(BizFeesChargedDTO bizFeesChargedDTO) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysDept dept=sysDeptMapper.selectDeptById(sysUser.getDeptId());
        if(!dept.getDeptName().equals("支付助手总部")){
            bizFeesChargedDTO.setOrgnname(dept.getDeptName());
            Long merchId = posMachineMapper.selectMerchId(sysUser.getUserId());
            bizFeesChargedDTO.setMerchId(merchId);
        }
        List<BizFeesChargedDTO> list = iBizMerchTransactionsService.tramcBilling(bizFeesChargedDTO);
        ExcelUtil<BizFeesChargedDTO> util = new ExcelUtil<BizFeesChargedDTO>(BizFeesChargedDTO.class);
        return util.exportExcel(list, "transactions");
    }
    /*查看分润*/

    @GetMapping("/listAlls/{uuId}")
    public AjaxResult listAlls(@PathVariable("uuId") String uuId) {
        List<BizProfitDeatail> list = iBizMerchTransactionsService.queryListAlls(uuId);
        return AjaxResult.success(list);
    }
    /*查看服务费返现*/

    @GetMapping("/listReturnAlls/{uuId}")
    public AjaxResult listReturnAlls(@PathVariable("uuId") String uuId) {
        List<CashbackRecordDTO> list = iBizMerchTransactionsService.listReturnAlls(uuId);
        return AjaxResult.success(list);
    }
    /*查看流量费返现*/

    @GetMapping("/listTramcBillingAlls/{uuId}")
    public AjaxResult listTramcBillingAlls(@PathVariable("uuId") String uuId) {
        List<CashbackRecordDTO> list = iBizMerchTransactionsService.listTramcBillingAlls(uuId);
        return AjaxResult.success(list);
    }
    /*借单开关*/

    @GetMapping("/stealSwitch")
    public AjaxResult listStealSwitch(StealSwitchDTO stealSwitchDTO) {
        StealSwitchDTO list = iBizMerchTransactionsService.listStealSwitch(stealSwitchDTO);
        return AjaxResult.success(list);
    }
    /*追溯开关*/

    @GetMapping("/ascendSwitch")
    public AjaxResult listAscendSwitch(AscendSwitchDTO ascendSwitchDTO) {
        AscendSwitchDTO list = iBizMerchTransactionsService.listAscendSwitch(ascendSwitchDTO);
        return AjaxResult.success(list);
    }
    /*借单修改*/
    @PostMapping("/stealupdate")
    public AjaxResult stealupdate(@RequestBody StealSwitchDTO stealSwitchDTO) {
        int list = iBizMerchTransactionsService.stealupdate(stealSwitchDTO);
        return AjaxResult.success(list);
    }
    /*追溯修改*/

    @PostMapping("/ascendupdate")
    public AjaxResult ascendupdate(@RequestBody AscendSwitchDTO ascendSwitchDTO) {
        int list = iBizMerchTransactionsService.ascendupdate(ascendSwitchDTO);
        return AjaxResult.success(list);
    }
    /*判断密码是否正确*/

    @PostMapping("/estimateIsPass")
    public AjaxResult estimateIsPass(@RequestBody EstimateIsPassDTO estimateIsPassDTO) {
        AjaxResult ajaxResult=AjaxResult.success();
        int list = iBizMerchTransactionsService.estimateIsPass(estimateIsPassDTO);
        if (list>0){
            ajaxResult.put("code",200);
            ajaxResult.put("msg","密码正确");
            return ajaxResult;
        }else {
            ajaxResult.put("code",500);
            ajaxResult.put("msg","密码错误，请重新输入！！！");
            return ajaxResult;
        }
    }
    /*修改高级设置密码*/

    @PostMapping("/estimateUpdatePass")
    public AjaxResult estimateUpdatePass(@RequestBody EstimateIsPassDTO estimateIsPassDTO) {
        AjaxResult ajaxResult=AjaxResult.success();
        int rows = iBizMerchTransactionsService.estimateIsPass(estimateIsPassDTO);
        if (rows>0){
            ajaxResult.put("code",500);
            ajaxResult.put("msg","密码已存在，请输入新密码！！！");
            return ajaxResult;
        }else {
            int list = iBizMerchTransactionsService.estimateUpdatePass(estimateIsPassDTO);
            if (list>0){
                ajaxResult.put("code",200);
                ajaxResult.put("msg","密码设置成功");
                return ajaxResult;
            }else {
                ajaxResult.put("code",500);
                ajaxResult.put("msg","密码错误，请重新输入！！！");
                return ajaxResult;
            }
        }
    }

    @GetMapping("/listssss")
    public AjaxResult listss() {
        startPage();
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listss();
        return AjaxResult.success("ok");
    }



    @Log(title = "服务费列表", businessType = BusinessType.EXPORT)
    @GetMapping("/lists/return/export")
    public AjaxResult exportReturn(BizMerchTransactions bizMerchTransactions) {
        List<BizMerchTransactions> list = iBizMerchTransactionsService.listsReturn(bizMerchTransactions);
        ExcelUtil<BizMerchTransactions> util = new ExcelUtil<BizMerchTransactions>(BizMerchTransactions.class);
        return util.exportExcel(list, "transactions");
    }

    /**
     * 获取商户交易记录详细信息
     */
    @GetMapping(value = "/{Id}")
    public AjaxResult getErrorCause(@PathVariable("Id") Long Id) {
        return AjaxResult.success(iBizMerchTransactionsService.getErrorCause(Id));
    }

    /**
     * 新增商户交易记录
     */
    @PreAuthorize("@ss.hasPermi('merchant:transactions:add')")
    @Log(title = "商户交易记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMerchTransactions bizMerchTransactions) {
        return toAjax(iBizMerchTransactionsService.save(bizMerchTransactions) ? 1 : 0);
    }

    /**
     * 修改商户交易记录
     */
    @PreAuthorize("@ss.hasPermi('merchant:transactions:edit')")
    @Log(title = "商户交易记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMerchTransactions bizMerchTransactions) {
        return toAjax(iBizMerchTransactionsService.updateById(bizMerchTransactions) ? 1 : 0);
    }

    /**
     * 删除商户交易记录
     */
    @PreAuthorize("@ss.hasPermi('merchant:transactions:remove')")
    @Log(title = "商户交易记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{trnIds}")
    public AjaxResult remove(@PathVariable Long[] trnIds) {
        return toAjax(iBizMerchTransactionsService.removeByIds(Arrays.asList(trnIds)) ? 1 : 0);
    }

    @ApiOperation("交易记录")
    @PostMapping("/record")
    public AppTableData geRecord(@RequestBody TransactionRecord transactionRecord) {
        appPage(transactionRecord.getPageNum(), transactionRecord.getPageSize());
        return getAppDataTableList(iBizMerchTransactionsService.findTransactionRecord(transactionRecord));
    }
    @ApiOperation("交易记录")
    @PostMapping("/recordNew")
    public AppTableData geRecordNew(@RequestBody TransactionRecord transactionRecord) {
        appPage(transactionRecord.getPageNum(), transactionRecord.getPageSize());
        return getAppDataTableList(iBizMerchTransactionsService.findTransactionRecordNew(transactionRecord));
    }
}
